package com.tky.conference.implInterfaces;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tky.conference.base.UrlConstant;
import com.tky.conference.dao.LoginInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.interfaces.HttpResultListener;
import com.tky.conference.interfaces.LoginInter;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by cll on 2015/12/7.
 */
public class LoginInfoImpl implements LoginInter {
    private String TAG = "LoginInfoImpl";
    private LoginInfo loginInfo;
    private FailRequest failRequestobj;
    private HttpResultListener httpResultListener;

    public LoginInfoImpl(HttpResultListener httpResultListener) {
        this.httpResultListener = httpResultListener;
    }
    @Override
    public void getLoginInfo(String account, String password) {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params=new RequestParams();
        params.put("account", account);
        params.put("password", password);
        client.setTimeout(UrlConstant.TIME_OUT);
        client.post(UrlConstant.LOGIN, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {


                System.out.println("success");
                Log.i(TAG, "succ");
                JSONArray jsonArr;
                JSONObject jsonObject;
                JSONObject failjsonobj;
                String data = null;
                try {
                    String str = new String(bytes, "UTF-8");
                    Log.i(TAG, str);
                    jsonObject= new JSONObject(str);
                    String s = jsonObject.getString("status");
                    Log.i(TAG, s);
                    if (Integer.parseInt(s) >0) {
                        failRequestobj = new FailRequest();
                        failRequestobj.setStatus(jsonObject.getString("status"));
                        failRequestobj.setMsg(jsonObject.getString("msg"));
                        httpResultListener.onResult(failRequestobj);
                    }else{
                        String dataobj=jsonObject.getString("data");
                        Log.i(TAG, dataobj);
                        JSONObject datao=new JSONObject(dataobj);
                        JSONArray dataarr=new JSONArray(datao.getString("list"));
                        Log.i(TAG, dataarr.toString());
                        for (int j = 0; j < dataarr.length(); j++) {
                            data = dataarr.get(j).toString();
                            jsonObject = new JSONObject(data);
                            loginInfo=new LoginInfo();
                            loginInfo.setDepid(jsonObject.getString("depid"));
                            loginInfo.setDepname(jsonObject.getString("depname"));
//                            loginInfo.setLogopath(jsonObject.getString("logopath"));
                            loginInfo.setOrgId(jsonObject.getString("orgId"));
                            loginInfo.setUsername(jsonObject.getString("username"));
                        }
                        httpResultListener.onResult(loginInfo);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                System.out.println("fail");
                try {
                    Log.i(TAG, "onFailure:" + bytes);
                    String str = new String(bytes, "UTF-8");
                    JSONObject jsonObject = new JSONObject(str);
                    Log.i("url", throwable.toString());
                    if ((jsonObject.getString("status").equals("1"))) {
                        failRequestobj = new FailRequest();
                        failRequestobj.setStatus(jsonObject.getString("status"));
                        failRequestobj.setMsg(jsonObject.getString("msg"));
                        httpResultListener.onResult(failRequestobj);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onStart() {
                super.onStart();
                httpResultListener.onStartRequest();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                httpResultListener.onFinish();
            }
        });
    }
}
